package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ManholeReportDao;
import cn.topevery.garden.entity.po.ManholeReport;
import cn.topevery.garden.entity.query.ManholeReportQuery;
import cn.topevery.garden.entity.vo.ManholeReportVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;


/**
 * 井盖信息上报表
 *
 * @author code-generate
 * @date 2021-08-17
 */
@Repository
public class ManholeReportDaoImpl extends BaseDaoImpl<ManholeReport> implements ManholeReportDao {
    @Override
    public <P extends DbCsp> List<ManholeReport> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ManholeReportVO> listPage(ManholeReportQuery query) {
        String table = "t_manhole_report tmr left join t_device_warning tdw on tmr.c_id = tdw.c_main_id and tdw.c_type = '23ace7c4-9123-44cf-91a1-7e546c5ea2f9'";

        String columns = "tmr.*,tdw.c_warning_status,tdw.c_event_code";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tmr.c_id desc");
        sqlQueryMate
                .where(true, "tmr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tmr.c_device_code", ScriptConditionEnum.Like, query.getCode())
                .where(query.getType() != null, "tmr.c_type", ScriptConditionEnum.Eq, query.getType());

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ManholeReportVO.class);
    }

    @Override
    public List<ManholeReportVO> getLeanReportListByCode(String code) {
        String sql = "select tmc.* from t_device_warning tdw left join t_manhole_report tmc on tdw.c_main_id = tmc.c_id where tdw.c_type = '23ace7c4-9123-44cf-91a1-7e546c5ea2f9' and tmc.c_lean_status = 1 and tdw.c_device_code = '" + code + "' order by tmc.c_db_create_date desc limit 10";
        AtomicReference<List<ManholeReportVO>> list = new AtomicReference<>(new ArrayList<>());
        run(sql, CommandType.Text, dbCommand -> {
            list.set(dbCommand.executeToList(ManholeReportVO.class));
        });
        return list.get();
    }

}
